package com.distribution.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.domain.AjaxResult;
import com.distribution.common.core.domain.entity.SysUser;
import com.distribution.common.core.page.TableDataInfo;
import com.distribution.common.utils.poi.ExcelUtil;
import com.distribution.system.domain.bo.CmccDaAccountDetailExportBO;
import com.distribution.system.domain.converter.AccountDetailConverter;
import com.distribution.system.domain.dto.AgentDTO;
import com.distribution.system.domain.dto.CmccDaAccountDetailAddDto;
import com.distribution.system.domain.dto.CmccDaAccountDetailQueryDto;
import com.distribution.system.domain.dto.CmccDaAccountDetailQueryReq;
import com.distribution.system.domain.entity.CmccDaAccountDetail;
import com.distribution.system.domain.entity.CmccDaOrder;
import com.distribution.system.mapper.SysUserMapper;
import com.distribution.system.service.CmccDaAccountDetailService;
import com.distribution.system.service.CmccDaOrderService;
import com.distribution.system.service.ISysUserService;
import com.distribution.system.utils.convert.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * (CmccDaAccountDetail)表控制层
 */
@RestController
@RequestMapping(value = "/cmccDaAccountDetail")
@Slf4j
public class CmccDaAccountDetailController extends BaseController {
    @Resource
    CmccDaAccountDetailService cmccDaAccountDetailService;
    @Resource
    CmccDaOrderService cmccDaOrderService;
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    ISysUserService userService;
    @Resource
    AccountDetailConverter accountDetailConverter;


    /**
     * 查看资金明细
     * @param cmccDaAccountDetailQueryReq 资金明细查询对象
     */
    @PostMapping("/selectCmccDaAccountDetail")
    public TableDataInfo selectCmccDaAccountDetail(@RequestBody CmccDaAccountDetailQueryReq cmccDaAccountDetailQueryReq) {
        CmccDaAccountDetailQueryDto cmccDaAccountDetailQueryDto = getCmccDaAccountDetailQueryDto(cmccDaAccountDetailQueryReq);
        startPage();
        List<CmccDaAccountDetail> cmccDaAccountDetails = cmccDaAccountDetailService.selectCmccDaAccountDetail(cmccDaAccountDetailQueryDto);
        return getDataTable(cmccDaAccountDetails);
    }
    /**
     * 添加资金明细
     * @param cmccDaAccountDetailAddDto 资金明细添加对象
     */
    @PreAuthorize("@ss.hasAnyRoles('operationSupervisor')")
    @Transactional
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated CmccDaAccountDetailAddDto cmccDaAccountDetailAddDto) {
        // 设置代理佣金
        Long userId = cmccDaAccountDetailAddDto.getSysUser().getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        if (sysUser == null) return error("用户不存在");
        sysUser.setCommission(cmccDaAccountDetailAddDto.getAfterAmount());
        sysUserMapper.updateUser(sysUser);
        cmccDaAccountDetailAddDto.setSysUser(sysUser);
        cmccDaAccountDetailService.addAccountDetail(cmccDaAccountDetailAddDto);
        return success();
    }

    /**
     * 添加资金明细
     * @param orderNo 订单编号
     */
    @GetMapping("/addCmccDaAccountDetail")
    public AjaxResult addCmccDaAccountDetail(String orderNo) {
        CmccDaOrder cmccDaOrder = cmccDaOrderService.getOne(new LambdaQueryWrapper<CmccDaOrder>().eq(CmccDaOrder::getOrderId, orderNo));
        if (cmccDaOrder == null) {
            return error();
        }
        cmccDaAccountDetailService.addAccountDetailByOrder(cmccDaOrder);
        return success();
    }


    @GetMapping("/updateCmccDaAccountDetail")
    public AjaxResult updateCmccDaAccountDetail(String orderNo) {
        cmccDaAccountDetailService.updateCmccDaAccountDetail(orderNo,null);
        return success();
    }


    @PostMapping("/export")
    public void export(HttpServletResponse response, CmccDaAccountDetailQueryReq cmccDaAccountDetailQueryReq) {
        CmccDaAccountDetailQueryDto cmccDaAccountDetailQueryDto = getCmccDaAccountDetailQueryDto(cmccDaAccountDetailQueryReq);
        List<CmccDaAccountDetail> cmccDaAccountDetails = cmccDaAccountDetailService.selectCmccDaAccountDetail(cmccDaAccountDetailQueryDto);
        List<CmccDaAccountDetailExportBO> cmccDaAccountDetailExportBOS = BeanUtils.copyBeanCollection(CmccDaAccountDetailExportBO.class, cmccDaAccountDetails);
        ExcelUtil<CmccDaAccountDetailExportBO> util = new ExcelUtil<>(CmccDaAccountDetailExportBO.class);
        util.exportExcel(response, cmccDaAccountDetailExportBOS, "资金明细数据");
    }
    private CmccDaAccountDetailQueryDto getCmccDaAccountDetailQueryDto(CmccDaAccountDetailQueryReq cmccDaAccountDetailQueryReq) {
        CmccDaAccountDetailQueryDto cmccDaAccountDetailQueryDto = accountDetailConverter.accountDetailListExportReqToDto(cmccDaAccountDetailQueryReq);
        Long userId = getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        cmccDaAccountDetailQueryDto.setCurrentSysUser(sysUser);
        if (sysUser.isOperationSupervisor()){
            AgentDTO agentDTO = new AgentDTO();
            agentDTO.setParentId(userId);
//            List<SysUser> sysUserList = sysUserMapper.selectAllUserListByParentId(agentDTO);
            List<SysUser> sysUserList = userService.selectUserListByParentId(agentDTO);
            cmccDaAccountDetailQueryDto.setUserIdList(sysUserList.stream().map(SysUser::getUserId).collect(Collectors.toList()));
        }else {
            cmccDaAccountDetailQueryDto.setUserIdList(new ArrayList<>());
        }
        return cmccDaAccountDetailQueryDto;
    }
}
